import { PanelContentProvider, PanelDataProvider, RegisterPanelDataCallback } from './interface';
import { panel } from './panel-global';

export interface DraggingItem {
  /**
   * 原父
   *
   * @author chitanda
   * @date 2022-02-18 17:02:38
   * @type {(PanelDataProvider | null)}
   */
  from: PanelDataProvider | null;
  /**
   * 放置父
   *
   * @author chitanda
   * @date 2022-02-18 17:02:50
   * @type {(PanelDataProvider | null)}
   */
  to: PanelDataProvider | null;
  /**
   * 当前数据
   *
   * @author chitanda
   * @date 2022-02-18 17:02:00
   * @type {(PanelDataProvider | null)}
   */
  item: PanelDataProvider | null;
}

/**
 * 面板实例全局控制器
 *
 * @author chitanda
 * @date 2022-02-16 20:02:46
 * @export
 * @class PanelController
 */
export abstract class PanelController {
  dragging: DraggingItem = {
    from: null,
    to: null,
    item: null,
  };

  draggingReset(): void {
    this.dragging = {
      from: null,
      to: null,
      item: null,
    };
  }

  abstract fetchDataItems(key: string): Promise<PanelDataProvider[]>;

  getDataProvider(key: string): RegisterPanelDataCallback {
    const callback = panel.getDataProvider(key);
    if (callback) {
      return callback;
    }
    throw new Error(`未找到数据适配器：<${key}>`);
  }

  getProvider(key: string): PanelContentProvider {
    const provider = panel.getProvider(key);
    if (provider) {
      return provider;
    }
    throw new Error(`未找到绘制适配器：<${key}>`);
  }
}
